package com.yunlu.viewmodeldemo;

import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProviders;

import com.yunlu.viewmodeldemo.viewmodel.TimerViewModel;

public class MainActivity extends AppCompatActivity {
    private TextView tvTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        iniComponent();
    }

    /**
     * 初始化View
     */
    private void initView() {
        tvTime = findViewById(R.id.tv_time);
    }

    private void iniComponent() {
        //通过ViewModelProviders得到ViewModel，如果ViewModel不存在就创建一个新的，如果已经存在就直接返回已经存在的
        TimerViewModel timerViewModel = ViewModelProviders.of(this).get(TimerViewModel.class);
        timerViewModel.setOnTimeChangeListener(new TimerViewModel.OnTimeChangeListener() {
            @Override
            public void onTimeChanged(final int second) {
                //更新UI界面
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tvTime.setText("TIME:" + second);
                    }
                });
            }
        });
        timerViewModel.startTiming();
    }
}